<?php
namespace gdl;

class gdlSms {

    protected $_error = 0;
    protected $setting = array();
    protected $_base_url = 'http://120.24.167.205/msg/HttpSendSM?'; //基础类短信请求地址
    protected $_notice_url = 'http://120.24.167.205/msg/HttpSendSM?'; //通知类短信请求地地址
    protected $_captcha_url = 'http://120.24.167.205/msg/HttpSendSM?'; //验证码类短信请求地址
    protected $_other_url = 'http://120.24.167.205/msg/HttpSendSM?'; //其它类短信请求地址

    public function __construct($setting) {
        $this->setting = $setting;
    }

    /**
     * 发送模板短信
     * @param    string     $type 短信通道 手机号码集合,用英文逗号分开
     * @param    array      $option['mobile':手机号码集合,用英文逗号分开,'content':短信内容]
     * @return   boolean
     */
    public function sendTemplateSMS($type = 'captcha', $option) {
        $data['account'] = $this->setting['appkey'];
        $data['pswd'] = $this->setting['secretKey'];
        $data['needstatus'] = true;
        //解析模板内容
        if ($option['tpl']) {
            foreach ($option['data'] as $key => $val) {
                $data['{' . $key . '}'] = $val;
            }
            $data['msg'] = strtr($option['tpl'], $data);
        } else {
            $data['msg'] = $option['tpl'];
        }
        $name = '_' . $type . '_url';
        $url = $this->$name . http_build_query($data);
        //遍历发送
        $mobile = explode(',', $option['mobile']);
        foreach ($mobile as $key => $val) {
            if (false === $this->_https_request($url . '&mobile=' . $val))
                return false;
        }
        return true;
    }

    protected function _https_request($url, $data = null) {
        if (function_exists('curl_init')) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_TIMEOUT, 20);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1);
            if (!empty($data)) {
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
			var_dump($url);exit;
            return $output;
        } else {
            $this->_error = '短信发送失败，请开启curl服务！';
            return false;
        }
    }

    public function getError() {
        return $this->_error;
    }

}

?>